package person.bangbang.im.AndroidginTest.Xml;

import java.io.IOException;
import java.io.StringBufferInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import person.bangbang.im.Androidgin.Util.XmlBuilder;

public class XmlBuilderTest extends TestCase{
	private XmlBuilder mBuilder;
	
	private final String XML_VERSION = "xml version=\"1.0\"";
	private final String ACCS = "accounts";
	private final String ACC = "account";
	private final String ATTR_AUTO_LOGIN = "autologin";
	private final String ATTR＿OOXX = "ooxx";
	
	private final String DATA = "ooxx";
	
	private DocumentBuilder mDocBuilder;
	                                             
	
	@Override
	protected void setUp() throws Exception {
		// TODO Auto-generated method stub
		super.setUp();
		mDocBuilder = DocumentBuilderFactory.newInstance()
        				.newDocumentBuilder();
		mBuilder = new XmlBuilder();
	}
	
	/**
	 * test with attr xmlbuilder.
	 */
	public void testAttributes() {
		mBuilder = new XmlBuilder();
		String[] attrs = new String[]{
				                      ATTR_AUTO_LOGIN
		                              };
		String[] values = new String[]{
                "true"
                };	
		mBuilder.appendVersion(XML_VERSION);
		mBuilder.append(ACC, DATA, attrs, values);
		String xml = mBuilder.toString();
		assertEquals("<?" + XML_VERSION + "?>" + "<account autologin=\"true\">ooxx</account>", xml);
		Document doc = null;
		try {
			 doc = mDocBuilder.parse(new StringBufferInputStream(xml));
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		NodeList l = doc.getElementsByTagName(ACC);
		String val = l.item(0).getLocalName();
		NamedNodeMap map = l.item(0).getAttributes();
		
		assertEquals("account", val);
		assertEquals("true", map.getNamedItem(ATTR_AUTO_LOGIN).getNodeValue());
	}
	
	/**
	 * Base64 ?
	 */
	public void testBase64() {
		mBuilder.append(ACC, DATA);
		String xml = mBuilder.toString();
		assertEquals("<account>ooxx</account>", xml);
	}
	
	/**
	 * Base64 ?
	 */	
	public void testBase64ByteArray() {
		Byte[] b = new Byte[]{'a','b','c'};
//		Byte[] bb = "abcd".toCharArray()b;
		mBuilder.append(ACC, b);
		String xml = mBuilder.toString();
		assertEquals("<account>abc</account>", xml);
	}
	

	public void testBase64ByteBinary() {
		// Binary? what is binary    (01010101)2
		String s = "0110"; 
//		Byte[] bb = "abcd".toCharArray()b;
		
		mBuilder.append(ACC, s);
		String xml = mBuilder.toString();
		assertEquals("<account>0110</account>", xml);
	}
}
